﻿using HardcoreGame.Common.Abstract;
using HardcoreGame.Common.Cores.Seasons;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ReLogic.Graphics;
using Terraria;
using Terraria.GameContent;
using Terraria.Localization;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;

namespace HardcoreGame.Common.Systems
{
    public class SeasonSystem : ModSystem
    {
        public Season currentSeason;
        public int dayCounter = 0;
        public const int daysPerSeason = 12;
        public bool isNewDayMessageDisplayed = false;
        public string newDayMessage = "";
        public int displayTimer = 0;
        public const int displayDuration = 300;
        public const int fadeDuration = 60;
        public override void Load()
        {
            if (!Main.dedServ)
            {
                currentSeason = new Spring();
            }
        }
        public override void PostUpdateWorld()
        {
            currentSeason.ApplySeasonEffect();
            if (Main.dayTime && Main.time == 0 && !isNewDayMessageDisplayed)
            {
                dayCounter++;
                if (dayCounter >= daysPerSeason)
                {
                    int nextSeasonIndex = (GetSeasonIndex(currentSeason) + 1) % 4;
                    currentSeason = GetSeasonByIndex(nextSeasonIndex);
                    dayCounter = 0;
                }
                newDayMessage = $"{currentSeason.Name} {dayCounter + 1}" + Language.GetTextValue("Mods.HardcoreGame.Season.Day");
                isNewDayMessageDisplayed = true;
                displayTimer = 0;
            }
            if (isNewDayMessageDisplayed)
            {
                displayTimer++;
                if (displayTimer >= displayDuration)
                {
                    isNewDayMessageDisplayed = false;
                }
            }
        }
        public override void PostDrawInterface(SpriteBatch spriteBatch)
        {
            if (isNewDayMessageDisplayed && !string.IsNullOrEmpty(newDayMessage))
            {
                float alpha;
                if (displayTimer < fadeDuration)
                {
                    alpha = (float)displayTimer / fadeDuration;
                }
                else if (displayTimer > displayDuration - fadeDuration)
                {
                    alpha = 1f - (float)(displayTimer - (displayDuration - fadeDuration)) / fadeDuration;
                }
                DynamicSpriteFont largeFont = FontAssets.DeathText.Value;
                Vector2 textSize = largeFont.MeasureString(newDayMessage);
                Vector2 position = new(
                    Main.screenWidth / 2 - textSize.X / 2,
                    Main.screenHeight / 4 - textSize.Y / 2
                );
                Color textColor = Color.White;
                spriteBatch.DrawString(largeFont, newDayMessage, position, textColor);
            }
        }
        public static int GetSeasonIndex(Season season)
        {
            return season switch
            {
                Spring => 0,
                Summer => 1,
                Autumn => 2,
                Winter => 3,
                _ => throw new System.NotImplementedException(),
            };
        }
        public static Season GetSeasonByIndex(int index)
        {
            return index switch
            {
                0 => new Spring(),
                1 => new Summer(),
                2 => new Autumn(),
                3 => new Winter(),
                _ => throw new System.NotImplementedException(),
            };
        }
        public override void SaveWorldData(TagCompound tag)
        {
            tag["currentSeasonIndex"] = GetSeasonIndex(currentSeason);
            tag["dayCounter"] = dayCounter;
        }
        public override void LoadWorldData(TagCompound tag)
        {
            int seasonIndex = tag.GetInt("currentSeasonIndex");
            currentSeason = GetSeasonByIndex(seasonIndex);
            dayCounter = tag.GetInt("dayCounter");
        }
    }
}
